﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Accord.Math;
using Accord.Statistics;
using System.IO;
using System.ComponentModel;
using System.Xml.Serialization;

namespace GameName2
{
    class GestureSequence : ICloneable
    {

        public List<string> ClassesGes { get; set; }
        private double[][] input;

        public List<Vector2> SourcePath { get; set; }

        public int Output { get; set; }

        public int RecognizedAs { get; set; }

        public string OutputName
        {
            get { return ClassesGes[Output]; }
        }

        public string RecognizedAsName
        {
            get { return RecognizedAs >= 0 ? ClassesGes[RecognizedAs] : "-"; }
        }
        public GestureSequence()
        {
            RecognizedAs = -1;
        }

       
        public double[][] Input
        {
            get
            {
                if (input == null)
                    input = Preprocessing(SourcePath);
                return input;
            }
        }
        public static double[][] Preprocessing(List<Vector2> seqGesture)
        {     
            //casting vector2 dengan double[][] untuk memasukkan koordinat setiap sequence
            double[][] result = new double[(int)seqGesture.Count][];
            for (int i = 0; i < seqGesture.Count; i++)
                result[i] = new double[] { seqGesture[i].X, seqGesture[i].Y };

            //hitung normalisasi z-score
            double[][] zScores = Accord.Statistics.Tools.ZScores(result);
#if USE_LOG
            using (StreamWriter w = File.AppendText("log.txt"))
            {
                Log("Testing Sequence", w,seqGesture,result,zScores);
            }
#endif
            //tambahkan z-score dengan 10
            return zScores.Add(10); 
        }
        public static int[][] CastVector2ToIntDouble(List<Vector2> output)
        {
            //casting vector2 dengan int[][]
            int[][] result = new int[(int)output.Count][];
            for (int i = 0; i < output.Count; i++)
                result[i] = new int[] { (int)output[i].X, (int)output[i].Y };

            return result;
        }

#if USE_LOG
        public static void Log(string logMessage, TextWriter w,List<Vector2> sequence,double[][] result,double[][] zscore)
        {
            w.Write("\r\nLog Entry : ");
            w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            w.WriteLine("  :");
            w.WriteLine("  :{0}", logMessage);
            w.WriteLine("-------------------------------");
            for (int i = 0; i < sequence.Count; i++)
                w.WriteLine("sequence X:{0} Y:{1}\n", sequence[i].X, sequence[i].Y);
            w.WriteLine("\n--------Z-Score------\n");
            for (int i = 0; i < zscore.Length; i++)
                w.WriteLine("z-score X:{0} Y:{1}\n", zscore[i].GetValue(0), zscore[i].GetValue(1));

        }
#endif
        public object Clone()
        {
            return MemberwiseClone();
        }
    }
}
